<template>
    <div>
        <h2 align="center">房型修改</h2>

        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>房型名称</td>
                    <td>
                        <input type="text" v-model="queryInfo.TypeName">
                    </td>
                </tr>

                <tr>
                    <td>单价（元）</td>
                    <td>
                        <input type="text" v-model="queryInfo.Price">
                    </td>
                </tr>

                <tr>
                    <td>室内面积（平方米）</td>
                    <td>
                        <input type="number" v-model="queryInfo.Area">
                    </td>
                </tr>

                <tr>
                    <td>最多入住人数</td>
                    <td>
                        <input type="text" v-model="queryInfo.MaxCount">
                    </td>
                </tr>

                <tr>
                    <td>图片上传</td>
                    <td>
                        <input type="file" @change="img">
                        <img :src="queryInfo.Photo" width="100" height="100" v-if="queryInfo.Photo!=''" alt="">
                    </td>
                </tr>


                <tr>
                    <td>房间描述</td>
                    <td>
                        <textarea cols="50" rows="5" v-model="queryInfo.Desc"></textarea>
                    </td>
                </tr>

                <tr>
                    <td>是否有窗</td>
                    <td>
                        <input type="radio" name="state" :value="0" v-model="queryInfo.IsWin">是
                        <input type="radio" name="state" :value="1" v-model="queryInfo.IsWin">否
                    </td>
                </tr>

                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="修改" class="btn btn-primary" @click="ok">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive,onMounted} from 'vue'
import axios from 'axios';
import {useRoute,useRouter} from 'vue-router'
const router=useRouter();
const route=useRoute();
import moment from 'moment';

onMounted(()=>{
    getFan();
})

//反填
const getFan=()=>{
    axios({
        method:'get',
        url:'/api/HouseType/FanTian',
        params:{
            id:route.query.id
        }
    })
    .then(res=>{
        console.log(res);
        Object.assign(queryInfo,res.data);
    })
    .catch(err=>{
        console.log(err);
    })
    
}

let queryInfo=reactive({
    "Id":route.query.id,
  "TypeName": "",
  "Price": '',
  "Area": '',
  "MaxCount": '',
  "Photo": "",
  "Desc": "",
  "IsWin": '',
})

//图片上传
const img=(e:any)=>{
    var obj=e.target.files[0];
    var f=new FormData();
    f.append('file',obj);

    axios({
        method:'post',
        url:'/api/HouseType/UpFileImg',
        data:f
    })
    .then(res=>{
        console.log(res);
        queryInfo.Photo=res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}
//添加
const ok=()=>{
    //有效性判断
    if(queryInfo.TypeName==""){
        alert('房型名称不能为空');
        return;
    }

    if(queryInfo.Price==""){
        alert('价格不能为空');
        return;
    }

    if(queryInfo.Area==""){
        alert('面积不能为空');
        return;
    }

    if(queryInfo.MaxCount==""){
        alert('最多入住人数不能为空');
        return;
    }

    if(queryInfo.Photo==""){
        alert('图片不能为空');
        return;
    }

    if(queryInfo.Desc==""){
        alert('房型描述不能为空');
        return;
    }
    if(queryInfo.IsWin===""){
        alert('是否有窗不能为空');
        return;
    }
    //返回网络请求
    axios({
        method:'put',
        url:'/api/HouseType/UpdType',
        data:queryInfo
    })
    .then(res=>{
        console.log(res);
        if(res.data==-1){
            alert('房型名称不能重复');
            return;
        }
        if(res.data>0){
            alert('成功');
            router.push('/');
        }
        else{
            alert('失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })

}
</script>

<style scoped>

</style>